﻿using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WOMPS
{
    public class Run
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public static bool Go()
        {
            if (logger.IsInfoEnabled)
            {
                logger.Info("Start Run");
            }
            try
            {
                List<SeriesFile> sfl = new List<SeriesFile>();
                //process each directory to get a list of SeriesFiles
                foreach (MonitoredDirectory dm in Globals.MonitoredDirectories)
                {
                    sfl.AddRange(FileAccess.ProcessDirectory(dm));
                }
                ////take SeriesFiles and turn them into Series with episodes
                List<Series> sl = FileAccess.GetSeries(sfl);
                //make email message
                string message = HTMLMaker.MakeEmail(sl);
                //send email
                if (message != null)
                {
                    EmailSend.Send(message);
                    FileAccess.CreatePrevious(message);
                }
            }
            catch(Exception exc)
            {
                logger.FatalException("WriteConfig", exc);
                return false;
            }
            if (logger.IsInfoEnabled)
            {
                logger.Info("End Run");
            }
            return true;
        }
    }
}
